package active;

import com.alibaba.fastjson.JSON;
import pojo.AjaxResult;
import pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

/**
 * @author 陈华旺
 * @version 1.0
 * @date 2023年11月24日 11:27
 * @Description TODO
 */
@WebServlet("/test01/request")
public class Test01Ajax extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        resp.setContentType("text/json;charset=utf-8");
//        resp.getWriter().println("OK");

        User u = new User(10001,"张三",0,"zhangsan","123456");
//        resp.getWriter().println(u.toString());
//        StringBuffer userJson = new StringBuffer()
//                .append("{")
//                .append("\"id\":").append(u.getId()).append(",")
//                .append("\"name\":\"").append(u.getName()).append("\",")
//                .append("\"sex\":").append(u.getSex()).append(",")
//                .append("\"username\":\"").append(u.getUsername()).append("\",")
//                .append("\"password\":\"").append(u.getPassword()).append("\"")
//                .append("}");

//        resp.getWriter().println(userJson.toString());
//        resp.getWriter().println( JSON.toJSONString(u) );

        AjaxResult result = new AjaxResult("查询成功",true,u,10001,new Date());
//        AjaxResult result = new AjaxResult("查询失败",false,null,10002,new Date());
        resp.getWriter().println( JSON.toJSONString( result ) );
    }
}
